<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/6
 * Time: 19:56
 */
namespace App\Http\Controllers\Api\V1;

use App\Http\Requests\Api\V1\UserLoginRequest;
use App\Http\Requests\Api\V1\UserRegisterRequest;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Zend\Diactoros\Response as Psr7Response;
use Psr\Http\Message\ServerRequestInterface;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\AuthorizationServer;


class UserController extends ApiController
{
    use AuthenticatesUsers;

//    public function register(UserRegisterRequest $request)
//    {
////        $verifyData = \Cache::get($request->verification_key);
////
////        if (!$verifyData) {
////            return $this->response->error('验证码已失效', 422);
////        }
//
////        if (!hash_equals((string)$verifyData['code'], $request->verification_code)) {
////            return $this->response->errorUnauthorized('验证码错误');
////        }
//
//        $user = User::create([
//            'name' => $request->name,
////            'phone' => $verifyData['phone'],
//            'password' => bcrypt($request->password),
//        ]);
//
//        // 清除验证码缓存
////        \Cache::forget($request->verification_key);
//        $data = [
//            'access_token' => Auth::guard('api')->fromUser($user),
//            'token_type' => 'Bearer',
//            'expires_in' => Auth::guard('api')->factory()->getTTL() * 60
//        ];
//        return $this->responseJson(0, $data);
//    }


    public function getAccessToken(AuthorizationServer $server, ServerRequestInterface $serverRequest)
    {
        $user = Auth::user();
        dd($user);
//        dd($server->respondToAccessTokenRequest($serverRequest, new Psr7Response));
        try {
            return $server->respondToAccessTokenRequest($serverRequest, new Psr7Response)->withStatus(201);
            dump($result);exit;
            $json = json_decode($result);
            dd($json);
            $user = Auth::user();
            return ;
        } catch (OAuthServerException $e) {
            //$e->getMessage()
            return $this->responseJson(10100);//用户名或密码不正确
        }
    }
    public function getUserInfo()
    {
        $user = Auth::user();
        return $user;
    }

    public function login(Request $request)
    {
//        $credentials['password'] = $request->password;
        $credentials = $request->only('email', 'password');
        if (!$token = Auth::guard()->attempt($credentials)) {
//            return $this->respondWithToken($token)->setStatusCode(201);
            return $this->responseJson(401, $this->respondWithToken($token));
        }
        return $this->responseJson(0);
    }

    protected function respondWithToken($token)
    {
        return [
            'access_token' => $token,
            'token_type' => 'Bearer',
            'expires_in' => Auth::guard('api')->factory()->getTTL() * 60
        ];
    }

//    public function logout()
//    {
//
//    }
//
//    public function verificationCode()
//    {
//
//    }
}